#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "musicwindow.h"
#include <QDebug>
#include <QWidget>
#include <QFrame>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QScreen>
#include <QRegion>
#include <QApplication>
#include <QCursor>
#include <QMouseEvent>
#include <QFile>
#include <QJsonDocument>
#include <QJsonObject>
#include <QString>
#include <QGuiApplication>
#include <QDir>
#include <QMessageBox>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    readConfigJson();
    initUI();    
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::initUI()
{
    //居中布局
    setCentralWidget(ui->widget);
    ui->gridLayout_3->setAlignment(Qt::AlignCenter);
    ui->widget->setLayout(ui->gridLayout_3);
    //密码框设置password格式
    ui->passWord->setEchoMode(QLineEdit::Password);
}

void MainWindow::writefigJson(const QString key, const bool value)
{
    //获取当前路径
    QString filePath = QCoreApplication::applicationDirPath(); //获取应用程序文件所在的目录
    QString configFilePath = QDir(filePath).filePath("bin/config.json"); //获取应用文件的完整目录，即文件所在的目录和json的位置
    QFile file(configFilePath);

    if(!file.open(QIODevice::ReadWrite  | QIODevice::Text)){
        return;
    }
    QByteArray jsonData = file.readAll();
    QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonData);
    if (jsonDoc.isNull()) {
        return;
    }
    if (!jsonDoc.isObject()) {
        qDebug() << "JSON 不是对象类型！";
        return;
    }
    QJsonObject jsonObj = jsonDoc.object();
    if(!jsonObj.contains(key)){
        jsonObj.insert(key,value);
    }else{
        jsonObj[key] = value;
    }

    //数据类型转换
    QJsonDocument doc(jsonObj);

    QByteArray jsonarr = doc.toJson(QJsonDocument::Indented);
    file.seek(0);
    file.write(jsonarr);
    file.close();
}

void MainWindow::on_login_clicked()
{
    QString username = ui->userName->text();
    QString password = ui->passWord->text();
    qDebug() << m_userName;
    qDebug() << ui->userName->text();
    if(username != m_userName){
        QMessageBox::critical(nullptr, "错误", "用户名错误！");
        return;
    }
    if(password != m_password){
        QMessageBox::critical(nullptr, "错误", "密码错误！");
        return;
    }
    MusicWindow *musicWindow = new MusicWindow;
    musicWindow->setAttribute(Qt::WA_DeleteOnClose);
    connect(this,&MainWindow::music,musicWindow,&MusicWindow::Music_a);
    emit music();
    this->close();
}

void MainWindow::mousePressEvent(QMouseEvent *event){
    if(event->button() == Qt::LeftButton){  //event->button()用于获取触发事件的鼠标按键,Qt::LeftButton是左键
        pull = true;
        pulls = event->pos(); //鼠标相对于窗口的位置
    }
}
void MainWindow::mouseMoveEvent(QMouseEvent *event){// |  ! &&
    if(pull && (event->buttons() & Qt::LeftButton)){  //判断正在用左键拖拽
        QPointF pos = event->globalPosition();
        // 如果你需要QPoint类型，可以转换一下
        QPoint newPos = pos.toPoint() - pulls;//计算拖拽后的位置
        move(newPos);
    }
}
void MainWindow::mouseReleaseEvent(QMouseEvent *event){
    if(event->button() == Qt::LeftButton){  //停止拖拽
        pull = false;
    }
}

void MainWindow::on_zhuce_clicked(){
    RegisterWindow *zc= new RegisterWindow;
    RegisterWindow r;
    QScreen *screen = QGuiApplication::primaryScreen();
    QRect rect = screen->geometry();
    int width = rect.width();
    int height = rect.height();
    zc->resize(width/4,height/3);
    zc->setFixedSize(zc->size());
    zc->setWindowFlags(Qt::FramelessWindowHint);

    zc->show();
    this->close();
}

void MainWindow::readConfigJson(){
    //获取当前路径
    QString filePath = QCoreApplication::applicationDirPath(); //获取应用程序文件所在的目录
    QString configFilePath = QDir(filePath).filePath("bin/config.json"); //获取应用文件的完整目录，即文件所在的目录和json的位置
    QFile file(configFilePath);
    // QFile file(filePath+"/bin/config.json");
    if(!file.open(QIODevice::ReadOnly | QIODevice::Text)){
        return;
    }
    QByteArray jsonData = file.readAll();
    file.close(); // 读取完成后关闭文件   文件操作 属于是 “锁” a b 玩电脑 a去玩 b只看 file.close

    QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonData);
    if (jsonDoc.isNull()) {
        return;
    }

    if (!jsonDoc.isObject()) {
        qDebug() << "JSON 不是对象类型！";
        return;
    }
    QJsonObject jsonObj = jsonDoc.object();
    m_userName = jsonObj["username"].toString();
    m_password = jsonObj["password"].toString();
    ui->userName->setText(m_userName);
    ui->readPass->setChecked(jsonObj["readPass"].toBool());
    if(ui->readPass->checkState()){
        ui->passWord->setText(m_password);
    }
}

void MainWindow::on_stop_clicked()
{
    QApplication::exit();
}


void MainWindow::on_readPass_checkStateChanged(const Qt::CheckState &arg1)
{
    //调用函数  把状态给他发过去
    writefigJson("readPass",arg1);
}

